package com.tanguy.user.ui.main

import android.graphics.Typeface
import android.support.v4.app.Fragment
import android.support.v4.content.ContextCompat
import android.text.Spannable
import android.text.SpannableString
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.tanguy.user.App
import com.tanguy.user.App.Companion.app
import com.tanguy.user.R
import com.tanguy.user.base.BottomTabActivity
import com.tanguy.user.bean.User
import com.tanguy.user.custom.CenterImageSpan
import com.tanguy.user.ui.me.activity.notice.NoticeActivity
import com.tanguy.user.utlis.extend.dp2px
import com.tanguy.user.utlis.extend.loginGuarantee
import org.jetbrains.anko.startActivity


class MainActivity : BottomTabActivity() {
    val arrayOfFragment: Array<Fragment> = arrayOf(
            TripFragment()//出行
            , HotelFragment()//酒店
            , MeFragment()//我的
    )

    override fun fragments(): Array<Fragment> = arrayOfFragment

    override fun setContentRes(): Int = R.layout.activity_main
    override fun initView() {
        super.initView()
        titleBar.showLeft(false)
        val rightButton = titleBar.addRightButton(null, 0, View.OnClickListener {
            //                        startActivity<WaitDriverActivity>()
//            startActivityForResult<WaitDriverActivity>(Const.RequestCode.WAIT_DRIVER, "orderId" to "20", "type" to 2)
            startActivity<NoticeActivity>()

        })
        rightButton.apply {
            setBackgroundResource(R.mipmap.xiaoxizhongxin)
            layoutParams.width = dp2px(22)
            layoutParams.height = dp2px(22)
//            setPadding(0, 0, dp2px(13), 0)
        }
        titleBar.setPadding(0, 0, dp2px(13), 0)
        App.get().connectSocket()
    }

    override fun onDestroy() {
        super.onDestroy()
        App.app.offSocket()
    }

    override fun getTabViewResID(position: Int): Int = R.layout.layout_bottom_tab_view

    override fun changeTab(view: View, position: Int, isSelected: Boolean) {

        if (isSelected) {
            changeBar(position)
        }

        val tabIconRes: Int
        val color: Int
        if (isSelected) {
            tabIconRes = mTabResPressed[position]
            color = R.color.color_4864f3
        } else {
            tabIconRes = mTabRes[position]
            color = R.color.color_d8c7f2
        }
        val tabIcon: ImageView = view.findViewById(R.id.tab_content_image)
        tabIcon.setImageResource(tabIconRes)
        val tabText: TextView = view.findViewById(R.id.tab_content_text)
        tabText.text = mTabTitle[position]
        tabText.setTextColor(ContextCompat.getColor(this, color))
    }

    private fun changeBar(position: Int) {

//设置不为加粗
        titleBar.titleView.typeface = Typeface.defaultFromStyle(Typeface.NORMAL)
        when (position) {
//            0 -> title = "唐吉"
            0 -> {
                titleBar.titleView.typeface = Typeface.defaultFromStyle(Typeface.BOLD)
                title = "唐吉"
            }
            1 -> title = "酒店"
            2 -> {
                loginGuarantee {
                    if (it == null) {
                        mTabLayout.getTabAt(0)?.select()
                        return@loginGuarantee
                    }
                    mTabLayout.getTabAt(2)?.select()
                    showMe(it)
                }
            }
        }
    }


    fun showMe(user: User) {
        val fragment = arrayOfFragment[2]
        if (fragment is MeFragment) {
            fragment.refreshUI(user)
        }
        val name = "${user.name} "
        val imgSpan = CenterImageSpan(this, if (user.sex == 1) R.mipmap.xingbie_nan else R.mipmap.xingbie_nv)
        val spanString = SpannableString("${name}icon")
        spanString.setSpan(imgSpan, name.length, name.length + 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        title = spanString
    }


    val mTabTitle = arrayOf("出行", "酒店", "我的")
    val mTabRes = intArrayOf(R.mipmap.tab_chuxing_weixuan, R.mipmap.tab_jiudian_weixuan, R.mipmap.tab_wode_weixuan)
    val mTabResPressed = intArrayOf(R.mipmap.tab_chuxing_xuanzhong, R.mipmap.tab_jiudian_xuanzhong, R.mipmap.tab_wode_xuanzhong)

    override fun onRestart() {
        super.onRestart()
        if (!app.isLogin) {
            mTabLayout.getTabAt(0)?.select()
            return
        }

    }

    private var firstTime: Long = 0
    override fun onBackPressed() {
        val secondTime = System.currentTimeMillis()
        if (secondTime - firstTime > 2000) {
            showToast("再按一次退出程序")
            firstTime = secondTime
        } else {
            super.onBackPressed()
        }
    }
}
